package com.bdqn.easybuy1.dao.impl;

import com.bdqn.easybuy1.dao.BaseDao;
import com.bdqn.easybuy1.dao.ProductCategoryDao;
import com.bdqn.easybuy1.entities.ProductCategory;
import com.bdqn.easybuy1.param.ProductCategoryParam;
import com.bdqn.easybuy1.utils.EmptyUtils;

import java.sql.Connection;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

public class ProductCategoryDaoImpl extends BaseDao<ProductCategory> implements ProductCategoryDao {
    /**
     * 根据id删除商品
     *
     * @param conn       数据库连接对象
     * @param categoryId 产品分类id
     * @return 返回受影响的行数
     * @throws Exception
     */
    @Override
    public int deleteById(Connection conn, Integer categoryId) throws Exception {
        String sql = " DELETE FROM `easybuy_product_category` WHERE `id` = ? ";
        return super.update(conn, sql, categoryId);
    }

    /**
     * 根据id查询商品分类
     *
     * @param conn 数据库连接对象
     * @param id   产品分类的id
     * @return 产品分类的信息对象
     * @throws Exception
     */
    @Override
    public ProductCategory queryProductCategoryById(Connection conn, Integer id) throws Exception {
        String sql = "SELECT `id`,`name`,`parentId`,`type`,`iconClass`  FROM `easybuy_product_category` WHERE `id` = ? ";
        return getBean(conn, sql, id);
    }

    /**
     * 添加商品分类
     *
     * @param conn            数据库连接对象
     * @param productCategory 封装产品类型信息的对象
     * @return 返回受影响的行数
     * @throws Exception
     */
    @Override
    public int insert(Connection conn, ProductCategory productCategory) throws Exception {
        String sql = " INSERT INTO `easybuy_product_category`(`name`,`parentId`,`type`,`iconClass`) VALUES (?,?,?,?) ";
        return super.update(conn, sql, productCategory.getName(),
                productCategory.getParentId(),
                productCategory.getType(),
                productCategory.getIconClass());
    }

    /**
     * 修改商品分类
     *
     * @param conn            数据库连接对象
     * @param productCategory 封装了修改产品分类信息的对象
     * @return 返回受影响的行数
     * @throws Exception
     */
    @Override
    public int update(Connection conn, ProductCategory productCategory) throws Exception {
        String sql = " UPDATE `easybuy_product_category` SET `name` = ?,`parentId` = ?,`type` = ?,`iconClass` = ? WHERE `id` = ?  ";
        return super.update(conn, sql, productCategory.getName(),
                productCategory.getParentId(),
                productCategory.getType(),
                productCategory.getIconClass(),
                productCategory.getId());
    }

    /**
     * 根据条件查询商品分类列表
     *
     * @param conn   数据库连接对象
     * @param params 查询参数
     * @return 返回查询的商品列表数据
     * @throws Exception
     */
    @Override
    public List<ProductCategory> queryProductCategoryList(Connection conn, ProductCategoryParam params) throws Exception {
        List<Object> objParams = new ArrayList<Object>();
        StringBuffer sqlBuffer = new StringBuffer("SELECT epc1.*,epc2.name as parentName FROM easybuy_product_category epc1 LEFT JOIN easybuy_product_category epc2 ON epc1.parentId=epc2.id where 1=1 ");

        if (EmptyUtils.isNotEmpty(params.getName())) {
            sqlBuffer.append(" AND epc1.`name` LIKE ? ");
            objParams.add("%" + params.getName() + "%");
        }
        if (EmptyUtils.isNotEmpty(params.getParentId())) {
            sqlBuffer.append(" AND epc1.`parentId` = ? ");
            objParams.add(params.getParentId());
        }
        if (EmptyUtils.isNotEmpty(params.getType())) {
            sqlBuffer.append(" AND epc1.`type` = ? ");
            objParams.add(params.getType());
        }
        if (params.isPage()) {
            sqlBuffer.append(" LIMIT  " + params.getStartIndex() + "," + params.getPageSize());
        }

        return getBeanList(conn, sqlBuffer.toString(), objParams.toArray());
    }

    /**
     * 根据参数查询商品分类的数目
     *
     * @param conn  数据库连接对象
     * @param param 查询参数
     * @return 返回查询数据的条数
     * @throws Exception
     */
    @Override
    public Integer queryProductCategoryCount(Connection conn, ProductCategoryParam param) throws Exception {
        List<Object> paramsList = new ArrayList<Object>();
        StringBuffer sql = new StringBuffer(" SELECT COUNT(*) FROM `easybuy_product_category` WHERE 1=1 ");
        if (EmptyUtils.isNotEmpty(param.getName())) {
            sql.append(" AND `name` LIKE ? ");
            paramsList.add("%" + param.getName() + "%");
        }
        if (EmptyUtils.isNotEmpty(param.getParentId())) {
            sql.append(" AND `parentId` = ? ");
            paramsList.add(param.getParentId());
        }

        Long retVal = super.getValue(conn, sql.toString(), paramsList.toArray());

        return retVal.intValue();
    }
}
